package MediaOnline.WebService;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpConnection;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import MediaOnline.GUI.Nahi_MediaActivity;
import MediaOnline.GUI.actListFilm;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;

public class HTTPTest {
/** Called when the activity is first created. */
    

	public Context mContext;
	public String url=null;
	public String key=null;
	ProgressDialog progress; 
		public HTTPTest(Context ctx, String url, String key){
			mContext = ctx;
			progress = new ProgressDialog(mContext);
			this.url = url;
			this.key = key;
		}
		
		public void startTest(){
			Test test = new Test();
			test.execute();
		}

	class Test extends AsyncTask<String, Void, String> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			//Chay progress dialog
			progress.setMessage("Http testing...");
			progress.setIndeterminate(true);
			progress.setCancelable(false);
			progress.show();
		}


		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			progress.dismiss();
			//actListFilm.PlayFilm();
		}


		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			
			
			HttpClient httpclient = new DefaultHttpClient();
		    HttpPost httppost = new HttpPost("http://hdtv.com.vn/Films/XemFilm.aspx");

		    try {
		        // Add your data
		        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
		        nameValuePairs.add(new BasicNameValuePair("pilot", url));
		        nameValuePairs.add(new BasicNameValuePair("flight", key));
		        Log.d("pilot/flight ::::::", url+"====="+key);
		        
	        
		        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		        
		        Log.d("POST execute", httpclient.execute(httppost).getParams().toString());
		        Log.d("POST URL", httppost.getEntity().getContent().toString());
		        
		        //ResponseHandler<String> responseHandler = new BasicResponseHandler();
		        HttpResponse response = httpclient.execute(httppost);
		        String u = httppost.getURI().toURL().getQuery();
		        Log.d("http Query:::::", u);
		        String u1 = httppost.getURI().toURL().getRef();
		        Log.d("http getRef:::::", u1);
		        String u2 = httppost.getURI().toURL().getContent().toString();
		        Log.d("http get content:::::", u2);
		        //httppost.getURI().toURL().openConnection().connect();
		        //Log.d("Path:::::", u);
		        
		        
		        
		        
		        //Gọi player
		        /*Intent intent = new Intent(mContext, Nahi_MediaActivity.class);
				Bundle newBundle = new Bundle();
				newBundle.putString("Link", httppost.getURI().toURL().toString());
				intent.putExtras(newBundle);
				mContext.startActivity(intent);*/
		        //httppost.
				

		        
		        // Execute HTTP Post Request
		        //HttpResponse response = httpclient.execute(httppost);
		        //response.
		        
		        
		        /*
		        
		        
		        
		        HttpEntity entity = response.getEntity();
		        if (entity != null) {
		            InputStream instream = entity.getContent();
		            	if (instream != null) {
		            	StringBuilder sb = new StringBuilder();
		            	String line;
		            	int i=0;
		            	try {
		            	BufferedReader reader = new BufferedReader(new InputStreamReader(instream, "UTF-8"));
		            	while ((line = reader.readLine()) != null) {
		            		
		            		Log.d("HTTPPOST Stream "+i+":::::", line);
		            		i++;
		            	}
		            	} finally {
		            		instream.close();
		            	}
		            	return sb.toString();
		            	} else {
		            	return "";
		            	}
		        }*/

		    } catch (Exception e) {
		        // TODO Auto-generated catch block
		    	Log.d("HTTPPOST exception::::::", e.toString());
		    } 
		    
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			/*DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost("http://hdtv.com.vn/check.aspx");
			BasicHttpParams params2 = new BasicHttpParams();
			params2.setParameter("para1", "abc");
			params2.setParameter("para2", "def");
		    httppost.setParams(params2);
			
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);  
			nameValuePairs.add(new BasicNameValuePair("para1", "12312"));  
			nameValuePairs.add(new BasicNameValuePair("para2", "234"));  
			
			
			try {
				httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 

			
			
			
		    Log.d("c Httppost params::::", httppost.getParams().getParameter("para1").toString());
			Log.d("C URI:::::", httppost.getURI().toString());
			
			try {
				HttpResponse response = httpClient.execute(httppost);
				Log.d("c Respond content::::",response.getEntity().getContent().toString());
				Header[] lstHeader = response.getAllHeaders();
				for (int i = 0; i < lstHeader.length; i++) {
					Log.d("c Respond header:::::", lstHeader[i].toString());
				}
				
				Log.d("C Respond::::::", response.toString());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				Log.d("C Error::::::", e.toString());
			}
			List<Cookie> cookies = httpClient.getCookieStore().getCookies();
			
			if (cookies.isEmpty()) {
				Log.d("Cookies::::::","Empty");
			} else {
			    for (int i = 0; i < cookies.size(); i++) {
			        Log.d("Cookies::::::","- " + cookies.get(i).toString());
			    }
			}*/
			return null;
		}
		
	}
}
